On occasion, we will wind up with a Flow
of Flow
objects. For example, we
might get a Flow
with results from a database query, and each row in those
results requires another call that results in another Flow
. In these cases,
it can be convenient to "flatten" the flow-of-flows into a single Flow
emitting
each of the nested results. There are two operators for that: flattenConcat()
and flattenMerge()
.
flattenConcat()
subscribes to the first of the inner Flow
objects.
flattenConcat()
emits each of items emitted by that inner Flow
, and
when that Flow
completes, flattenConcat()
subscribes to the next of the
inner Flow
objects. This continues until the outer Flow
itself completes.
Here, flowRepeat()
emits the same object a specified number of times, with a
10-millisecond delay between each emission. That Flow
will be run on
Dispatchers.Default
, courtesy of flowOn()
. We wrap four of those flowRepeat()
calls in an outer Flow
and use flattenConcat()
on it. The result is that we
get all 10 of the 1
items, followed by all 10 of the 2
items, etc.